# Copyright 2021 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load(
    "//pw_build:compatibility.bzl",
    "host_backend_alias",
    "incompatible_with_mcu",
)
load("//pw_build:pw_facade.bzl", "pw_facade")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
    features = ["-layering_check"],
)

licenses(["notice"])

pw_facade(
    name = "binary_semaphore",
    srcs = [
        "binary_semaphore.cc",
    ],
    hdrs = [
        "public/pw_sync/binary_semaphore.h",
    ],
    backend = ":binary_semaphore_backend",
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        "//pw_chrono:system_clock",
        "//pw_preprocessor",
    ],
)

label_flag(
    name = "binary_semaphore_backend",
    build_setting_default = ":binary_semaphore_unspecified_backend",
)

host_backend_alias(
    name = "binary_semaphore_unspecified_backend",
    backend = "//pw_sync_stl:binary_semaphore",
)

pw_facade(
    name = "counting_semaphore",
    srcs = [
        "counting_semaphore.cc",
    ],
    hdrs = [
        "public/pw_sync/counting_semaphore.h",
    ],
    backend = ":counting_semaphore_backend",
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        "//pw_chrono:system_clock",
        "//pw_preprocessor",
    ],
)

label_flag(
    name = "counting_semaphore_backend",
    build_setting_default = ":counting_semaphore_unspecified_backend",
)

host_backend_alias(
    name = "counting_semaphore_unspecified_backend",
    backend = "//pw_sync_stl:counting_semaphore",
)

cc_library(
    name = "lock_annotations",
    hdrs = [
        "public/pw_sync/lock_annotations.h",
    ],
    strip_include_prefix = "public",
    deps = [
        "//pw_preprocessor",
    ],
)

cc_library(
    name = "lock_traits",
    hdrs = [
        "public/pw_sync/lock_traits.h",
    ],
    strip_include_prefix = "public",
)

cc_library(
    name = "borrow",
    hdrs = [
        "public/pw_sync/borrow.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":lock_annotations",
        ":lock_traits",
        ":virtual_basic_lockable",
        "//pw_assert:assert",
    ],
)

cc_library(
    name = "timed_borrow",
    hdrs = [
        "public/pw_sync/timed_borrow.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":borrow",
        ":lock_annotations",
        ":lock_traits",
        ":virtual_basic_lockable",
        "//pw_chrono:system_clock",
    ],
)

cc_library(
    name = "inline_borrowable",
    hdrs = [
        "public/pw_sync/inline_borrowable.h",
        "public/pw_sync/internal/borrowable_storage.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":borrow",
        ":mutex",
        ":virtual_basic_lockable",
    ],
)

cc_library(
    name = "virtual_basic_lockable",
    hdrs = [
        "public/pw_sync/virtual_basic_lockable.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":lock_annotations",
        "//pw_polyfill",
    ],
)

pw_facade(
    name = "mutex",
    srcs = [
        "mutex.cc",
    ],
    hdrs = [
        "public/pw_sync/mutex.h",
    ],
    backend = ":mutex_backend",
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        ":lock_annotations",
        ":virtual_basic_lockable",
        "//pw_preprocessor",
    ],
)

label_flag(
    name = "mutex_backend",
    build_setting_default = ":mutex_unspecified_backend",
)

host_backend_alias(
    name = "mutex_unspecified_backend",
    backend = "//pw_sync_stl:mutex",
)

pw_facade(
    name = "timed_mutex",
    srcs = [
        "timed_mutex.cc",
    ],
    hdrs = [
        "public/pw_sync/timed_mutex.h",
    ],
    backend = ":timed_mutex_backend",
    strip_include_prefix = "public",
    deps = [
        ":lock_annotations",
        ":mutex",
        ":virtual_basic_lockable",
        "//pw_chrono:system_clock",
        "//pw_preprocessor",
    ],
)

label_flag(
    name = "timed_mutex_backend",
    build_setting_default = ":timed_mutex_unspecified_backend",
)

host_backend_alias(
    name = "timed_mutex_unspecified_backend",
    backend = "//pw_sync_stl:timed_mutex",
)

pw_facade(
    name = "recursive_mutex",
    srcs = ["recursive_mutex.cc"],
    hdrs = ["public/pw_sync/recursive_mutex.h"],
    backend = ":recursive_mutex_backend",
    strip_include_prefix = "public",
    deps = [
        ":lock_annotations",
        "//pw_preprocessor",
    ],
)

label_flag(
    name = "recursive_mutex_backend",
    build_setting_default = ":recursive_mutex_unspecified_backend",
)

host_backend_alias(
    name = "recursive_mutex_unspecified_backend",
    backend = "//pw_sync_stl:recursive_mutex",
)

pw_facade(
    name = "interrupt_spin_lock",
    srcs = [
        "interrupt_spin_lock.cc",
    ],
    hdrs = [
        "public/pw_sync/interrupt_spin_lock.h",
    ],
    backend = ":interrupt_spin_lock_backend",
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        ":lock_annotations",
        ":virtual_basic_lockable",
        "//pw_preprocessor",
    ],
)

label_flag(
    name = "interrupt_spin_lock_backend",
    build_setting_default = ":interrupt_spin_lock_unspecified_backend",
)

host_backend_alias(
    name = "interrupt_spin_lock_unspecified_backend",
    backend = "//pw_sync_stl:interrupt_spin_lock",
)

pw_facade(
    name = "thread_notification",
    hdrs = [
        "public/pw_sync/thread_notification.h",
    ],
    backend = ":thread_notification_backend",
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        "//pw_chrono:system_clock",
    ],
)

label_flag(
    name = "thread_notification_backend",
    build_setting_default = ":thread_notification_unspecified_backend",
)

host_backend_alias(
    name = "thread_notification_unspecified_backend",
    backend = ":binary_semaphore_thread_notification_backend",
)

pw_facade(
    name = "timed_thread_notification",
    hdrs = [
        "public/pw_sync/timed_thread_notification.h",
    ],
    backend = ":timed_thread_notification_backend",
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        ":thread_notification",
        "//pw_chrono:system_clock",
    ],
)

label_flag(
    name = "timed_thread_notification_backend",
    build_setting_default = ":timed_thread_notification_unspecified_backend",
)

host_backend_alias(
    name = "timed_thread_notification_unspecified_backend",
    backend = ":binary_semaphore_timed_thread_notification_backend",
)

cc_library(
    name = "binary_semaphore_thread_notification_backend",
    hdrs = [
        "public/pw_sync/backends/binary_semaphore_thread_notification_inline.h",
        "public/pw_sync/backends/binary_semaphore_thread_notification_native.h",
        "public_overrides/pw_sync_backend/thread_notification_inline.h",
        "public_overrides/pw_sync_backend/thread_notification_native.h",
    ],
    includes = [
        "public",
        "public_overrides",
    ],
    tags = ["noclangtidy"],
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":binary_semaphore",
        ":thread_notification.facade",
    ],
)

cc_library(
    name = "binary_semaphore_timed_thread_notification_backend",
    hdrs = [
        "public/pw_sync/backends/binary_semaphore_timed_thread_notification_inline.h",
        "public_overrides/pw_sync_backend/timed_thread_notification_inline.h",
    ],
    includes = [
        "public",
        "public_overrides",
    ],
    tags = ["noclangtidy"],
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":binary_semaphore_thread_notification_backend",
        ":timed_thread_notification.facade",
        "//pw_chrono:system_clock",
    ],
)

cc_library(
    name = "yield_core",
    hdrs = [
        "public/pw_sync/yield_core.h",
    ],
    strip_include_prefix = "public",
)

cc_library(
    name = "condition_variable_facade",
    hdrs = [
        "public/pw_sync/condition_variable.h",
    ],
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        "//pw_chrono:system_clock",
        "//pw_sync:mutex",
    ],
)

# TODO: b/228998350 - This needs to be instantiated for each platform that
# provides an implementation of $dir_pw_thread:test_threads and
# $dir_pw_sync:condition_variable.
# pw_cc_library(
#     name = "condition_variable_test",
#     srcs = ["condition_variable_test.cc"],
#     deps = [
#         ":condition_variable_facade",
#         "//pw_containers:vector",
#         "//pw_sync:mutex",
#         "//pw_sync:timed_thread_notification",
#         "//pw_thread:sleep",
#         "//pw_thread:non_portable_test_thread_options",
#         "//pw_thread:thread",
#         "//pw_unit_test",
#     ],
# )
#
# Filegroup to mark `condition_variable_test.cc` as used for the linter:
filegroup(
    name = "condition_variable_test_filegroup",
    srcs = ["condition_variable_test.cc"],
)

cc_library(
    name = "threaded_testing",
    testonly = True,
    hdrs = ["public/pw_sync/internal/threaded_testing.h"],
    strip_include_prefix = "public",
    deps = [
        ":thread_notification",
        ":virtual_basic_lockable",
        "//pw_thread:test_thread_context",
        "//pw_thread:thread",
        "//pw_unit_test",
    ],
)

cc_library(
    name = "lock_testing",
    srcs = ["lock_testing.cc"],
    hdrs = ["public/pw_sync/lock_testing.h"],
    implementation_deps = ["//pw_assert:check"],
    strip_include_prefix = "public",
    deps = [
        ":virtual_basic_lockable",
        "//pw_chrono:system_clock",
    ],
)

cc_library(
    name = "borrow_testing",
    hdrs = ["public/pw_sync/borrow_testing.h"],
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        ":borrow",
        ":lock_traits",
    ],
)

cc_library(
    name = "timed_borrow_testing",
    hdrs = ["public/pw_sync/timed_borrow_testing.h"],
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        ":borrow_testing",
        ":timed_borrow",
    ],
)

pw_cc_test(
    name = "lock_traits_test",
    srcs = ["lock_traits_test.cc"],
    deps = [
        ":lock_testing",
        ":lock_traits",
    ],
)

pw_cc_test(
    name = "borrow_test",
    srcs = ["borrow_test.cc"],
    deps = [
        ":borrow",
        ":lock_testing",
        ":timed_borrow_testing",
    ],
)

pw_cc_test(
    name = "inline_borrowable_test",
    srcs = [
        "inline_borrowable_test.cc",
    ],
    deps = [
        ":inline_borrowable",
        ":interrupt_spin_lock",
        ":lock_annotations",
        ":mutex",
    ],
)

pw_cc_test(
    name = "binary_semaphore_facade_test",
    srcs = [
        "binary_semaphore_facade_test.cc",
        "binary_semaphore_facade_test_c.c",
    ],
    deps = [
        ":binary_semaphore",
        "//pw_preprocessor",
    ],
)

pw_cc_test(
    name = "counting_semaphore_facade_test",
    srcs = [
        "counting_semaphore_facade_test.cc",
        "counting_semaphore_facade_test_c.c",
    ],
    deps = [
        ":counting_semaphore",
        "//pw_preprocessor",
    ],
)

pw_cc_test(
    name = "mutex_facade_test",
    srcs = [
        "mutex_facade_test.cc",
        "mutex_facade_test_c.c",
    ],
    deps = [
        ":borrow_testing",
        ":mutex",
        "//pw_preprocessor",
    ],
)

pw_cc_test(
    name = "timed_mutex_facade_test",
    srcs = [
        "timed_mutex_facade_test.cc",
        "timed_mutex_facade_test_c.c",
    ],
    deps = [
        ":timed_borrow_testing",
        ":timed_mutex",
        "//pw_chrono:system_clock",
        "//pw_preprocessor",
    ],
)

pw_cc_test(
    name = "recursive_mutex_facade_test",
    srcs = [
        "recursive_mutex_facade_test.cc",
        "recursive_mutex_facade_test_c.c",
    ],
    deps = [
        ":recursive_mutex",
        "//pw_preprocessor",
    ],
)

pw_cc_test(
    name = "interrupt_spin_lock_facade_test",
    srcs = [
        "interrupt_spin_lock_facade_test.cc",
        "interrupt_spin_lock_facade_test_c.c",
    ],
    # TODO: https://pwbug.dev/325509758 - Doesn't work on the Pico yet; hangs
    # indefinitely.
    target_compatible_with = select({
        "//pw_build/constraints/chipset:rp2040": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    deps = [
        ":borrow_testing",
        ":interrupt_spin_lock",
        "//pw_preprocessor",
    ],
)

pw_cc_test(
    name = "thread_notification_facade_test",
    srcs = [
        "thread_notification_facade_test.cc",
    ],
    deps = [
        ":thread_notification",
        ":threaded_testing",
    ],
)

pw_cc_test(
    name = "timed_thread_notification_facade_test",
    srcs = [
        "timed_thread_notification_facade_test.cc",
    ],
    deps = [
        ":timed_thread_notification",
        "//pw_chrono:system_clock",
    ],
)

filegroup(
    name = "doxygen",
    srcs = [
        "public/pw_sync/binary_semaphore.h",
        "public/pw_sync/borrow.h",
        "public/pw_sync/counting_semaphore.h",
        "public/pw_sync/inline_borrowable.h",
        "public/pw_sync/interrupt_spin_lock.h",
        "public/pw_sync/lock_annotations.h",
        "public/pw_sync/mutex.h",
        "public/pw_sync/thread_notification.h",
        "public/pw_sync/timed_borrow.h",
        "public/pw_sync/timed_mutex.h",
        "public/pw_sync/timed_thread_notification.h",
        "public/pw_sync/virtual_basic_lockable.h",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "backends.rst",
        "docs.rst",
    ],
    prefix = "pw_sync/",
    target_compatible_with = incompatible_with_mcu(),
)
